package com.wcf.proxy.demo;

import sun.misc.ProxyGenerator;

import java.io.FileOutputStream;
import java.lang.reflect.Proxy;

/**
 * Created by hadoop on 2016/1/31.
 *
 * 代理类调用被代理类
 */
public class ProxyRun {
    public static void main(String[] args) {
        SubjectImpl proxidClass = new SubjectImpl();
        Subject proxySubject = (Subject)Proxy.newProxyInstance(Subject.class.getClassLoader(), new Class[]{Subject.class}, new ProxyHandler(proxidClass));
        proxySubject.doSomething();

        //write proxySubject class binary data to file
//        createProxyClassFile();
    }
    public static void createProxyClassFile(){
        String name = "ProxySubject";
        byte[] data = ProxyGenerator.generateProxyClass(name, new Class[]{Subject.class});
        try
        {
            FileOutputStream out = new FileOutputStream( name + ".class" );
            out.write( data );
            out.close();
        }
        catch( Exception e )
        {
            e.printStackTrace();
        }
    }

}
